<?php

namespace app\api\model;

use think\Exception;
use think\Model;

/**
 * Bill
 */
class Bill extends Model
{
    // 表名
    protected $name = 'member_bill';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;


    public function addBill($member,$income,$title,$type){
         $model = new self();
         $data['member_id'] =$member['id'];
         $data['type'] =$type;
         $data['title'] =$title;
         $data['income'] =$income;
         $data['before_income'] =$member['residue_integral'];
         if($type == 1){
             $data['after_income'] =$member['residue_integral']+ $income;
         }else{
             $data['after_income'] =$member['residue_integral'] -  $income;
         }
         if (!$model->save($data)) {
            return false;
        }
        return true;
    }


    public function getUserIntegral($params){
        $member_id = $params['member_id'] ?? 0;
        $list = self::where(['member_id' => $member_id])
            ->field('id,title,income,type,create_time')
            ->paginate(10);
        foreach($list as &$val){
            $val['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
        }
        return api_message($list);
    }
}